package com.das.mechanic_base.widget;

import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.adapter.common.X3SelectBrandNewAdapter;
import com.das.mechanic_base.base.X3BaseBottomSheetDialog;
import com.das.mechanic_base.bean.main.CarBrandBean;
import com.das.mechanic_base.bean.main.CarModelSelectBean;
import com.das.mechanic_base.utils.LanguageConvent;
import com.das.mechanic_base.utils.X3PinyinComparator;
import com.das.mechanic_base.utils.X3ScreenUtils;
import com.das.mechanic_base.utils.X3StringUtils;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class X3HomeNameSelectDialog extends X3BaseBottomSheetDialog implements View.OnClickListener, X3SelectBrandNewAdapter.IOnSelectStyle, X3SlideBar.onTouchLetterListener {

    private TextView tv_title;
    private TextView tv_cancel;
    private RecyclerView rlv_brand;
    private ProgressBar pb_bar;
    private X3SlideBar sb_bar;
    private TextView tv_num;
    private LinearLayout ll_bottom;
    private RelativeLayout rl_bottom;

    private List<CarBrandBean> mList = new ArrayList<>();
    private X3SelectBrandNewAdapter styleAdapter;
    private X3PinyinComparator pinyinComparator;
    private List<String> headerList;

    public X3HomeNameSelectDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected boolean getDialogCancel() {
        return false;
    }

    @Override
    protected void initView() {
        tv_title = getView(R.id.tv_title);
        tv_cancel = getView(R.id.tv_cancel);
        pb_bar = getView(R.id.pb_bar);
        rlv_brand = getView(R.id.rlv_brand);
        sb_bar = getView(R.id.sb_bar);
        tv_num = getView(R.id.tv_num);
        ll_bottom = getView(R.id.ll_bottom);
        rl_bottom = getView(R.id.rl_bottom);

        tv_cancel.setOnClickListener(this);

        pinyinComparator = new X3PinyinComparator();

        sb_bar.setOnTouchLetterListener(this);

        rlv_brand.setLayoutManager(new LinearLayoutManager(mContext));
        rlv_brand.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

                LinearLayoutManager layoutManager = (LinearLayoutManager) rlv_brand.getLayoutManager();
                int itemPosition = layoutManager.findFirstCompletelyVisibleItemPosition();
                if (itemPosition < 0 || itemPosition >= mList.size()) {
                    return;
                }
                String sortLetters = mList.get(itemPosition).getSortLetters();
                if (X3StringUtils.isEmpty(sortLetters)) {
                    return;
                }
                for (int i = 0; i < headerList.size(); i++) {
                    if (sortLetters.equalsIgnoreCase(headerList.get(i))) {
                        if (sb_bar != null) {
                            sb_bar.changeSelectIndex(i);
                        }
                        break;
                    }
                }
            }
        });

        configBottomParams();

    }

    @Override
    protected int getLayoutId() {
        return R.layout.x3_home_brand_number_picker;
    }

    private void configBottomParams(){
        ll_bottom.setVisibility(View.GONE);
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) rl_bottom.getLayoutParams();
        lp.setMargins(lp.leftMargin,lp.topMargin,lp.rightMargin, X3ScreenUtils.dip2px(20,getContext()));
        rl_bottom.setLayoutParams(lp);
    }

    public void changeData(String title, List<CarBrandBean> dataLists) {
        pb_bar.setVisibility(View.GONE);
        rlv_brand.setVisibility(View.VISIBLE);
        tv_title.setText(title);

//        mList = characterParser.filledData(dataLists);
        mList = LanguageConvent.filledData(dataLists);
        /**
         * 对数据进行A-Z排序
         */
        Collections.sort(mList,pinyinComparator);

        headerList = new ArrayList<>();

        for (CarBrandBean carBrandBean : mList) {
            headerList.add(carBrandBean.getSortLetters());
        }

        Set<String> set = new LinkedHashSet<>(headerList);
        headerList = new ArrayList<>(set);
        if (sb_bar != null) {
            sb_bar.changeNum(headerList);
        }

        rlv_brand.addItemDecoration(new X3UserItemDecoration());
        List<CarModelSelectBean> selectLists = new ArrayList<>();
        styleAdapter = new X3SelectBrandNewAdapter(mContext,selectLists);
        rlv_brand.setAdapter(styleAdapter);
        styleAdapter.changeData(mList,true);
        styleAdapter.setiOnSelectStyle(this);
    }

    @Override
    public void show() {
        super.show();
        rlv_brand.setVisibility(View.GONE);
        pb_bar.setVisibility(View.VISIBLE);

    }


    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.tv_cancel) {
            dismiss();
        }
    }

    IOnClickCar iOnClick;

    public void setiOnClick(IOnClickCar iOnClick) {
        this.iOnClick = iOnClick;
    }

    @Override
    public void iOnSelectStyleName(CarBrandBean brandBean,boolean isSearchStyle) {
        dismiss();
        if (iOnClick != null) {
            iOnClick.iOnClickSelect(brandBean);
        }
    }

    @Override
    public void onTouchLetterChange(String letter, boolean touch) {
        if (touch) {
            tv_num.setText(letter);
            tv_num.setVisibility(View.VISIBLE);
        }else {
            tv_num.setVisibility(View.GONE);
        }
        for (int i = 0; i < mList.size(); i++) {
            if (letter.equalsIgnoreCase(mList.get(i).getSortLetters())) {
                ((LinearLayoutManager)rlv_brand.getLayoutManager()).scrollToPositionWithOffset(i,0);
//                ((LinearLayoutManager)rlv_brand.getLayoutManager()).setStackFromEnd(true);
                return;
            }
        }

    }

    public interface IOnClickCar {
        void iOnClickSelect(CarBrandBean brandBean);
    }

    @Override
    protected void onStart() {
        super.onStart();
        MobclickAgent.onPageStart("车主姓氏弹窗");
    }

    @Override
    protected void onStop() {
        super.onStop();
        MobclickAgent.onPageEnd("车主姓氏弹窗");
    }
}
